package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("/roles")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping
    public Result list(String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("列表查询: {} {} {} ", name, page, pageSize);
        PageResult<Role> pageResult = roleService.list(name, page, pageSize);
        return Result.success(pageResult);
    }


    @PostMapping
    public Result add(@RequestBody Role role){
        log.info("新增角色: {}", role);
        roleService.add(role);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询: {}", id);
        Role role = roleService.getById(id);
        return Result.success(role);
    }


    @PutMapping
    public Result update(@RequestBody Role role){
        log.info("更改角色: {}", role);
        roleService.update(role);
        return Result.success();
    }


    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除角色: {}", id);
        roleService.deleteById(id);
        return Result.success();
    }


    @GetMapping("/list")
    public Result listAll(){
        log.info("查询所有角色");
        return Result.success(roleService.listAll());
    }


}
